import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'home',
  },
  //首页
  {
    path: '/home',
    name: 'home',
    component:HomeView
    
  },
  //收藏
  {
    path:'/collection',
    name:'collection',
    component:() => import("../views/collectionView.vue")
  },
  //行程
  {
    path:'/itinerary',
    name:'itinerary',
    component:() => import("../views/itineraryView.vue")
  },
  //我的
  {
    path:'/mi',
    name:'mi',
    component:() => import("../views/miView.vue"),
    // beforeEnter:(to,from,next)=>{
    //   let token = window.localStorage.getItem("token");
    //   if(!token){
    //     next("/login");
    //     return;
    //   }else{
    //     next();
    //   }
    // }
  },
  //登陆
  {
    path:'/login',
    name:'login',
    component: () => import("../views/loginView.vue"),
     // 独享路由守卫
    
  },
  //日期
  {
    path:'/data',
    name:'data',
    component:() => import("../views/dataView.vue")
  },
  //搜索
  {
    path:'/search',
    name:'search',
    component: () => import("../views/SearchView.vue")
  },
  //位置
  {
    path:'/location',
    name:'location',
    component: () => import("../views/locationView.vue")
  },
  //详情页
  {
    path:'/page',
    name:'page',
    component:() => import("../views/PageView.vue")
  },
 
  //详情二
  {
    path:'/pagetwo',
    name:'pagetwo',
    component:() => import("../views/pageTwo.vue")
  },
  //身份证
  {
    path:'/card',
    name:'card',
    component:() => import("../views/cardView.vue")
  },
  //凹凸币
  {
    path:'/coins',
    name:'coins',
    component:() => import("../views/coinsView.vue")
  },
  //余额
  {
    path:'/balance',
    name:'balance',
    component:() => import("../views/balanceView.vue")
  },
  //账单
  {
    path:'/bill',
    name:'bill',
    component:() => import("../views/BillView.vue")
  },
  //开票
  {
    path:'/billing',
    name:'billing',
    component:() => import("../views/BillingView.vue")
  },
  //设置
  {
    path:'/setup',
    name:'setup',
    component:() => import("../views/SetUp.vue")
  },
  //搜索车
  {
    path:'/sear',
    name:'sear',
    component:() => import("../views/searView.vue")
  },
  //租用
  {
    path:'/rent',
    name:'rent',
    component:() => import("../views/rentView.vue")
  }
 
]

// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
  
  return originalPush.call(this,location).catch(err=>err);
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior: function () {
     return { x: 0, y: 0 }
    }
})

router.beforeEach((to,from,next)=>{
  let token = window.localStorage.getItem("token");
  // 登录界面判断
  if(to.path == "/login"){
      next();
      return;
  }else{ //判断是否登录
    if(!token){
      next("/login");//进入登录页面
      return;
    }else{
      next();//执行下一个路由
    }
  }
})

export default router
